'use strict';

const authConst = require('../constant/auth-const');
const Controller = require('egg').Controller;

class AdminController extends Controller {

    async addRouterAuth() {
        const { app, ctx, service } = this;
        const { body } = ctx.request;
        const { title, path, name, type, component, parent_id } = body;

        if (!title) {
            return ctx.errorResult('title missing');
        }

        if (!path) {
            return ctx.errorResult('path missing');
        }

        if (!name) {
            return ctx.errorResult('name missing');
        }

        if (type > 1) {
            if (!component) {
                return ctx.errorResult('component missing');
            }

            if (!parent_id) {
                return ctx.errorResult('parent_id missing');
            }
        }

        const sqlQuery = {
            title,
            path,
            name
        };

        try {
            const [result, created] = await app.model.RouterAuth.findCreateFind({
                where: {
                    ...sqlQuery,
                    ...{
                        type,
                        parent_id
                    }
                },
                defaults: body
            });

            if (created) {
                ctx.successResult(result);
            } else {
                ctx.errorResult('router exist');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async editRouterAuth() {
        const { app, ctx, service } = this;
        const { body } = ctx.request;
        const { routerId } = ctx.params;

        try {
            const result = await app.model.RouterAuth.update(body, {
                where: {
                    id: routerId
                }
            });
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getRouterAuth() {
        const { app, ctx, service } = this;
        const { query } = ctx;

        try {
            const result = await app.model.RouterAuth.findAll({
                where: {
                    ...query
                }
            });
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async delRouterAuth() {
        const { app, ctx, service } = this;
        const { routerId } = ctx.params;

        try {
            const result = await app.model.RouterAuth.destroy({
                where: {
                    id: routerId
                }
            });
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getBindRoleRouter() {
        const { app, ctx, service } = this;
        const { roleId, type, parentId } = ctx.query;

        if (!roleId) {
            return ctx.errorResult('roleId missing');
        }

        if (!type) {
            return ctx.errorResult('type missing');
        }

        const routerType = Number(type);
        if (routerType > authConst.authType.oneLevelRouter.value && !parentId) {
            return ctx.errorResult('parentId missing');
        }

        try {
            const result = await service.roleAuth.getBindRoleRouterSrv(roleId, routerType, parentId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getUnbindRoleRouter() {
        const { app, ctx, service } = this;
        const { roleId, type, parentId } = ctx.query;

        if (!roleId) {
            return ctx.errorResult('roleId missing');
        }

        if (!type) {
            return ctx.errorResult('type missing');
        }

        const routerType = Number(type);
        if (routerType > 1 && !parentId) {
            return ctx.errorResult('parentId missing');
        }

        try {
            const result = await service.roleAuth.getUnbindRoleRouterSrv(roleId, routerType, parentId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async bindRoleRouter() {
        const { app, ctx, service } = this;
        const { roleId, type, parentId } = ctx.query;
        const { body } = ctx.request;
        const { authArray } = body;

        if (!roleId) {
            return ctx.errorResult('roleId missing');
        }

        if (!type) {
            return ctx.errorResult('type missing');
        }


        const routerType = Number(type);
        if (routerType > 1 && !parentId) {
            return ctx.errorResult('parentId missing');
        }

        try {
            const result = await service.roleAuth.bindRoleRouterSrv(roleId, routerType, authArray, parentId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async unbindRoleRouter() {
        const { app, ctx, service } = this;
        const { authId } = ctx.params;

        if (!authId) {
            return ctx.errorResult('authId missing');
        }

        try {
            const result = await service.roleAuth.unbindRoleRouterSrv(authId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getAllAuthRouter() {
        const { app, ctx, service } = this;

        try {
            const result = await service.roleAuth.getAllAuthRouterSrv();
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getRoleAuthRouter() {
        const { app, ctx, service } = this;
        const { roleId } = ctx.params;

        if (!roleId) {
            return ctx.errorResult('roleId missing');
        }

        try {
            const result = await service.roleAuth.getRoleAuthRouterSrv(roleId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }
}

module.exports = AdminController;
